<style>
  /*#purchasePlan-edit .layui-table-body {*/
  /*    margin-right: -17px;*/
  /*}*/
  .layui-layer-title {
    display: flex;
    align-items: center;
  }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label">订单编号</label>
                  <div class="layui-input-inline mr0">
                    <input name="warehouseOutboundOrderCode" class="layui-input" type="text" placeholder="请输入订单编号" />
                  </div>
                </div>
                <div class="layui-inline">
                  <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
              <div class="query-rt"></div>
            </div>
          </div>
        </div>

        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button id="purchasePlanBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <table class="layui-table" id="purchasePlanTable" lay-filter="purchasePlanTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchasePlanTableBar">
  <a class="layui-btn layui-btn-xs layui-btn" lay-event="detail">详情</a>

  {{# if(d.status == 1) { }}
<a class="layui-btn layui-btn-xs layui-btn" lay-event="audit">受理</a>
  {{# } }}
  {{# if(d.status == 3) { }}
<a class="layui-btn layui-btn-xs layui-btn" lay-event="termination">发货</a>
  {{# } }}

</script>

<style>
  #purchasePlan-form .layui-disabled {
    color: #393d49 !important;
  }
</style>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "util", "table", "admin", "laytpl", "laydate", "tableTreeDj"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var util = layui.util;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laytpl = layui.laytpl;
    var laydate = layui.laydate;
    var tableTree = layui.tableTreeDj;

    // 渲染表格
    var insTb = table.render({
      elem: "#purchasePlanTable",
      url: config.base + "warehouse/warehouse/admin/outboundOrder/list",
      toolbar: true,
      defaultToolbar: ["filter"],
      where: { access_token: config.getToken().access_token },
      page: true,
      cellMinWidth: 100,

      cols: [
        [
          { type: "numbers" },
          { field: "warehouseOutboundOrderCode", align: "center", title: "订单编号" },
          { field: "billLadingNumber", align: "center", title: "客户提单号" },
          {
            field: "sources",
            align: "center",
            title: "数据来源",
            templet: function (d) {
              return d.sources === 1 ? "平台" : "后台";
            }
          },
          { field: "numberOfMaterials", align: "center", title: "物料数量" },
          { field: "customerId", align: "center", title: "客户代码" },
          { field: "consignee", align: "center", title: "联系人" },
          { field: "totalGrossWeight", align: "center", title: "总毛重" },
          { field: "totalNetWeight", align: "center", title: "总净重" },
          //状态：1.待受理，2.发货中，3.已受理，4.已撤回 5.出库完成
          {
            field: "status",
            align: "center",
            title: "状态",
            templet: function (d) {
              return d.status === 1 ? "待受理" : d.status === 2 ? "发货中" : d.status === 3 ? "已受理" :  d.status === 5 ? "出库完成" : "已撤回";
            }
          },
          // { field: "createTime", align: "center", title: "创建时间" },
          { field: "creatorName", align: "center", title: "创建人" },
          { align: "center", toolbar: "#purchasePlanTableBar", title: "操作" , width: 240}
          //
        ]
      ]
    });

    // 工具条点击事件
    table.on("tool(purchasePlanTable)", function (obj) {
      var data = obj.data;
      var layEvent = obj.event;
      if (layEvent === "detail") {
        admin.req(
          "warehouse/warehouse/admin/outboundOrder/selectOrderDetail",
          {
            id: data.id
          },
          (res) => {
            if (res.code === 200) {
              openFormFun("detail", res.data);
            }
          }
        );
      }  else if (layEvent === "audit") {
        // 确认是否受理通过
        layer.open({
          title: "受理",
          content: "是否受理通过？",
          btn: ["受理通过", "关闭"],
          yes: function (index) {
            console.log(index);
            admin.req("warehouse/warehouse/admin/outboundOrder/updateOutboundOrder", { id: data.id, status: 3 }, (res) => {
              if (res.code === 200) {
                layer.msg("受理通过");
                layer.close(index);
                insTb.reload();
              }
            });
          }
        });
      }   else if (layEvent === "termination") {
        // 确认是否受理通过
        window.location.href = "/admin/index.html#/warehouse/product/warehouseOutboundRecord"; // 替换为目标地址
      }

    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", function (data) {
      insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
    });

    form.render("select");

    // 添加按钮点击事件
    $("#purchasePlanBtnAdd").click(function () {
      openFormFun("add");
    });

    function openFormFun(eventName, data) {
      admin.formOpen({
        area: "90%",
        offset: "10%",
        title: "出库订单详情",
        path: "components/warehouse/product/warehouseOutboundOrderForm.html",
        // path: "components/warehouse/product/warehouseOutboundOrderFormq.html",
        success: function () {
          parentData.eventName = eventName;
          parentData.data = data;
        }
      });
    }
  });
</script>
